package com.group6.exam.manager.dao;

import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.UserInfoEntity;
import com.group6.exam.common.util.CollectionUtil;
import com.group6.exam.common.util.DBUtil;

public class ManagerDaoImpl implements IManagerDao {

	@Override
	public boolean login(UserInfoEntity user) {

		String sql = "SELECT 1 FROM userInfo WHERE account=? AND password=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql,
				user.getAccount(), user.getPassword());

		return CollectionUtil.isEmpty(list);
	}

	@Override
	public List<Map<String, Object>> getMainMenus(String username) {
		String sql = "select * from rightInfo where "
				+ "right_id in (select right_id from roleRightInfo where "
				+ "role_id=(select role_id from userinfo where account=?)) "
				+ "and parent_id='root'";

		return DBUtil.executeQuery(sql, username);
	}

	@Override
	public List<Map<String, Object>> getChildMenus(String username) {
		String sql = "select * from rightInfo where "
				+ "right_id in (select right_id from roleRightInfo where "
				+ "role_id=(select role_id from userinfo where account=?)) "
				+ "and parent_id<>'root'";

		return DBUtil.executeQuery(sql, username);
	}

	@Override
	public int addUser(UserInfoEntity user) {
		String sql = "insert into userinfo values(null,?,?,?,?)";
		Object[] parameters = { user.getAccount(), user.getPassword(),
				user.getRoleId(), user.getUsername() };

		return DBUtil.executeUpdate(sql, parameters);
	}

	@Override
	public List<Map<String, Object>> getUserInfo() {
		return DBUtil.executeQuery("select * from userinfo");
	}

	@Override
	public int deleteUserbyId(String userId) {
		return DBUtil.executeUpdate("delete from userInfo where user_id=?",
				userId);
	}

	@Override
	public Map<String, Object> getUserInfoById(String userId) {
		String sql = "select * from userInfo where user_id=?";

		List<Map<String, Object>> list = DBUtil.executeQuery(sql, userId);

		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public int updateUserbyId(UserInfoEntity user) {
		String sql = "update userInfo set " + "account=?," + "password=?,"
				+ "role_id=?," + "username=? " + "where user_id=?";
		Object[] parameters = { user.getAccount(), user.getPassword(),
				user.getRoleId(), user.getUsername(), user.getId() };

		return DBUtil.executeUpdate(sql, parameters);
	}

	@Override
	public int getTotalRecords() {
		return DBUtil.executeQuery("select * from userInfo").size();
	}

	@Override
	public List<Map<String, Object>> check(PageModel pageModel) {
		String sql = "select * from userInfo LIMIT ?,?";
		int index = (pageModel.getCurrentPage() - 1) * pageModel.getPageSize();
		int pageSize = pageModel.getPageSize();
		return DBUtil.executeQuery(sql, index, pageSize);
	}

	@Override
	public String getRoleName(String role_id) {
		String sql = "select role_name from roleInfo where role_id = ?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, role_id);
		return list != null && list.size() > 0 ? list.get(0).get("role_name")
				+ "" : null;
	}
}
